/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sample.util;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import sample.model.ServerConfigurationModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static sample.util.SSHUtils.execCommandByJSch;


/**
 *
 * @author wutc
 */
public class GetBaseInfo implements Callable<ServerConfigurationModel> {

//    private String[] serverBaseInfo = new String[9];

    private HashMap<String, Session> serverSessionMap = new HashMap<String, Session>();
    private HashMap<String, String[]> serverMap = new HashMap<String, String[]>();
    private static Boolean flag ;
    private Iterator it ;
    private int mapsize ;
    private String[] keys;
    private ServerConfigurationModel serverConfiguration;
    int keynum ;
//    DefaultTableModel model = new DefaultTableModel();

    public GetBaseInfo(HashMap serverMap,HashMap serverSessionMap,int keynum){
        this.serverMap = serverMap;
        this.serverSessionMap = serverSessionMap;
        this.mapsize = serverSessionMap.size();
        this.it = serverSessionMap.keySet().iterator();
        this.keynum = keynum;
        this.flag = true;
        this.keys = new String[mapsize];
        for (int i = 0; i < mapsize; i++) {
            keys[i] = it.next().toString();
        }
    }


    @Override
    public ServerConfigurationModel call() {
        String[] serverBaseInfo2 =  new String[10];
        while (flag){
            try {
                serverBaseInfo2 = getinfo();
                serverConfiguration = new ServerConfigurationModel(serverBaseInfo2[0],serverBaseInfo2[1],serverBaseInfo2[2],serverBaseInfo2[3],serverBaseInfo2[4],serverBaseInfo2[5],serverBaseInfo2[6],serverBaseInfo2[7],serverBaseInfo2[8],serverBaseInfo2[9]);
            }catch (Exception e){
                System.out.println("连接异常:"+e.getMessage()+"请重新连接再试");
            }
        }
        return serverConfiguration;
    }

     private String[] getinfo() throws IOException, JSchException, InterruptedException {
        String[] serverBaseInfo = new String[10];
        String str = getMapskey();
        //如果serverInfo中该ip设定为忽略则不执行命令
         if(Integer.parseInt(serverMap.get(str)[4])==1){
             //判断serverSessionMap 中是否还存在数据
             if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("LINUX")){
                 serverBaseInfo[0] = serverMap.get(str)[0];
                 serverBaseInfo[1] = str;
                 serverBaseInfo[2] = execCommandByJSch(serverSessionMap.get(str), "cat /proc/cpuinfo| grep \"physical id\"| sort| uniq| wc -l", "UTF-8") + "/" + execCommandByJSch(serverSessionMap.get(str), "cat /proc/cpuinfo |grep \"cores\"|uniq|awk -F: '{print $2}'", "UTF-8") + "/" + execCommandByJSch(serverSessionMap.get(str), "cat /proc/cpuinfo| grep \"processor\"| wc -l", "UTF-8");
                 serverBaseInfo[2] = ResultFormat(serverBaseInfo[2]);
                 serverBaseInfo[3] = execCommandByJSch(serverSessionMap.get(str), "echo \"`cat /proc/meminfo | grep MemTotal|awk '{print $2}'`/1024\"|bc", "UTF-8");
                 serverBaseInfo[4] = execCommandByJSch(serverSessionMap.get(str), "df -h|awk '{print $5,$6}'|sort -rn|head -1", "UTF-8");
                 serverBaseInfo[5] = execCommandByJSch(serverSessionMap.get(str), "java -version 2>&1 | sed '1!d' | sed -e 's/\"//g' | awk '{print $3}'", "UTF-8");
                 serverBaseInfo[6] = execCommandByJSch(serverSessionMap.get(str), "uname -a|awk '{print $2}'", "UTF-8");
                 serverBaseInfo[7] = execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8");
                 serverBaseInfo[8] = execCommandByJSch(serverSessionMap.get(str), "date", "UTF-8");//时间
                 serverBaseInfo[9] = execCommandByJSch(serverSessionMap.get(str), "cat /etc/*-release|grep PRETTY_NAME|awk -F \"=\" '{print $2}'", "UTF-8");//版本

             }
             if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("AIX")){
                 serverBaseInfo[0] = serverMap.get(str)[0];
                 serverBaseInfo[1] = str;
                 serverBaseInfo[2] = execCommandByJSch(serverSessionMap.get(str), "prtconf|grep Processors|awk -F \" \" '{print $4}'", "UTF-8") + "/" +  " "+ "/" + execCommandByJSch(serverSessionMap.get(str), "vmstat|grep lcpu|awk -F \"=\" '{print $2}'|awk -F \" \" '{print $1}'", "UTF-8");
                 serverBaseInfo[2] = ResultFormat(serverBaseInfo[2]);
                 serverBaseInfo[3] = execCommandByJSch(serverSessionMap.get(str), "vmstat |grep lcpu|awk -F \"=\" '{print $3}'", "UTF-8");
                 serverBaseInfo[4] = execCommandByJSch(serverSessionMap.get(str), "df -g|awk '{print $6,$7}'|sort -rn|head -1", "UTF-8");
                 serverBaseInfo[5] = execCommandByJSch(serverSessionMap.get(str), "java -version 2>&1 | sed '1!d' | sed -e 's/\"//g' | awk '{print $3}'", "UTF-8");
                 serverBaseInfo[6] = execCommandByJSch(serverSessionMap.get(str), "uname -a|awk '{print $2}'", "UTF-8");
                 serverBaseInfo[7] = execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8");
                 serverBaseInfo[8] = execCommandByJSch(serverSessionMap.get(str), "date", "UTF-8");//时间
                 serverBaseInfo[9] = execCommandByJSch(serverSessionMap.get(str), "oslevel -s", "UTF-8");
             }
         }else if(Integer.parseInt(serverMap.get(str)[4])==0){
             serverBaseInfo[0] = serverMap.get(str)[0];
             serverBaseInfo[1] = str;
             serverBaseInfo[2] = "-";
             serverBaseInfo[3] = "-";
             serverBaseInfo[4] = "-";
             serverBaseInfo[5] = "-";
             serverBaseInfo[6] = "-";
             serverBaseInfo[7] = "-";
             serverBaseInfo[8] = "-";
             serverBaseInfo[9] = "-";
         }

            Thread.sleep(50);
         //System.out.println("线程"+Thread.currentThread().getName()+"获取了服务器数据"+",此时的 keynum是:"+keynum );
        return serverBaseInfo;
    }

    public synchronized String getMapskey(){
        String key = keys[keynum];
        keynum++;
        if(keynum >= mapsize){
            flag = false;
        }
       // System.out.println("线程"+Thread.currentThread().getName()+"获取了key"+key+",此时的 keynum已经是:"+keynum );
        return key;
    }
    //去掉一个字符串中的换行符、回车符等，将连续多个空格替换成一个空格
    public static String ResultFormat(String string){
        Pattern p = Pattern.compile("\t|\r|\n");
        Matcher m = p.matcher(string);
        string = m.replaceAll("");
        string = string.replaceAll(" +", " ");
        return string;
    }



}


